package cn.gok.homework;

import java.util.*;
import java.util.stream.Stream;

/*
1 创建Book类： int bid（编号）,String bname,float price


        2 创建 TreeMap<Book,String> map;加入5本书，
        map.put(new Book(1,"西游记",100),"老白");
        要求按书的价格从高到低排序输出。
        使用两个遍历方法：  keySet遍历，entrySet遍历



        3  创建   ArrayList<Book> list;添加5本书，
        使用 stream运算，将价格低于150元的书，按编号从低到高输出。
        提示    filter(?).sort(new BookCompartor()).forEach(...)

*/
class Book{
    private int bid;         //编号
    private String bname;
    private float price;

    public Book(int bid, String bname, float price) {
        this.bid = bid;
        this.bname = bname;
        this.price = price;
    }

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bid=" + bid +
                ", bname='" + bname + '\'' +
                ", price=" + price +
                '}';
    }
}
public class Homework6 {
    public static void main(String[] args) {
        作业一();
        作业二();
    }
    /*
 3  创建   ArrayList<Book> list;添加5本书，
    使用 stream运算，将价格低于150元的书，按编号从低到高输出。
    提示    filter(?).sort(new BookCompartor()).forEach(...)

     */
    private static void 作业二() {
        ArrayList<Book> list = new ArrayList<>();
        list.add(new Book(1, "西游记", 100));
        list.add(new Book(2, "a", 161));
        list.add(new Book(3, "b", 90));
        list.add(new Book(4, "c", 99));
        list.add(new Book(5, "d", 25));

        Stream<Book> b1 = list.stream();
        b1.filter(book -> book.getPrice()<150).sorted(new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                if (o1.getBid()<o2.getBid()){
                    return -1;
                }else if (o1.getBid()==o2.getBid()&&o1.getBname().equals(o2.getBname()))
                {
                    return 0;
                }
                return 1;
            }
        }).distinct().forEach(System.out::println);
    }
    /*
 2 创建 TreeMap<Book,String> map;加入5本书，
            map.put(new Book(1,"西游记",100),"老白");
    要求按书的价格从高到低排序输出。
    使用两个遍历方法：  keySet遍历，entrySet遍历

     */
    private static void 作业一() {
        Map<Book,String> map = new TreeMap<>(new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                if (o1.getPrice()>o2.getPrice()){
                    return -1;
                }else if (o1.getPrice()==o2.getPrice()&&o1.getBname().equals(o2.getBname())){
                    return 0;
                }
                return 1;
            }
        });
        map.put(new Book(1,"西游记",100),"老白");
        map.put(new Book(2,"a",101),"老白");
        map.put(new Book(3,"b",90),"老C");
        map.put(new Book(4,"c",99),"老D");
        map.put(new Book(5,"d",25),"老E");

        for (Book book:map.keySet()){
            String value = map.get(book);
            System.out.println(book+"---"+value);
        }

        System.out.println("----------------------");

        for(Map.Entry<Book,String> e:map.entrySet()){
            Book key = e.getKey();
            String value = e.getValue();
            System.out.println(key+"---"+value);
        }
        System.out.println("----------------------");
    }
}
